package com.easy.controller;

import com.easy.bean.Medicine;
import com.easy.service.MedicineService;
import com.easy.util.Page;
import com.easy.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin
@RequestMapping("medicine")
public class MedicineController {

    @Autowired
    private MedicineService medicineService;

    // 分页查询（放在动态路径前）
    @GetMapping("page")
    public Result page(Page page, Medicine medicine) {
        Object obj = medicineService.page(page, medicine);
        return Result.success(obj);
    }

    // 新增药品
    @PostMapping
    public Result add(@RequestBody Medicine medicine) {
        Object obj = medicineService.add(medicine);
        return Result.success(obj);
    }

    // 编辑药品
    @PutMapping
    public Result edit(@RequestBody Medicine medicine) {
        Object obj = medicineService.edit(medicine);
        return Result.success(obj);
    }

    // 删除药品
    @DeleteMapping("{id}")
    public Result del(@PathVariable("id") Integer id) {
        Object obj = medicineService.del(id);
        return Result.success(obj);
    }

    // 根据ID查询药品
    @GetMapping("{id}")
    public Result one(@PathVariable("id") Integer id) {
        Object obj = medicineService.one(id);
        return Result.success(obj);
    }

    // 查询所有药品
    @GetMapping("all")
    public Result all(Medicine medicine) {
        Object obj = medicineService.all(medicine);
        return Result.success(obj);
    }
}
